package com.ruoyi.scheme.service.decision.Impl;

import com.ruoyi.scheme.pojo.Plant;
import com.ruoyi.scheme.pojo.PlatFormAndSlopeEntity;
import com.ruoyi.scheme.pojo.RepairMeasure;
import com.ruoyi.scheme.service.decision.DroolsRuleEngineService;
import org.kie.api.KieBase;
import org.kie.api.runtime.KieSession;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Drools 规则引擎服务实现类
 */
@Service
public class DroolsRuleEngineServiceImpl implements DroolsRuleEngineService {

    @Override
    public List<RepairMeasure> executeRules(KieBase kieBase, int locationType, PlatFormAndSlopeEntity platFormAndSlopeEntity) {
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(platFormAndSlopeEntity);
        // 初始化并设置全局变量，确保规则中使用的finalActions不为null
        List<RepairMeasure> actions = new ArrayList<>();
        kieSession.setGlobal("actions", actions);
        kieSession.setGlobal("finalActions", actions);
        kieSession.fireAllRules();
        kieSession.dispose();
        return actions; // 返回收集到的修复措施
    }
    
    // 保持原有方法的兼容性
    public List<RepairMeasure> executeRules(KieBase kieBase, PlatFormAndSlopeEntity platFormAndSlopeEntity) {
        return executeRules(kieBase, platFormAndSlopeEntity.getLocation(), platFormAndSlopeEntity);
    }

    @Override
    public Plant executePlantDensityRules(KieBase kieBase, Plant plant, PlatFormAndSlopeEntity platFormAndSlopeEntity) {
        System.out.println("执行Drools规则计算植物密度，植物名称: " + plant.getPlantName());
        
        KieSession kieSession = kieBase.newKieSession();
        kieSession.insert(plant);
        // 只插入需要的对象，platFormAndSlopeEntity在free.drl规则中可能不需要
        
        // 初始化actions列表作为全局变量，防止规则中使用时出现NullPointerException
        List<RepairMeasure> actions = new ArrayList<>();
        kieSession.setGlobal("actions", actions);
        kieSession.setGlobal("finalActions", actions);
        
        kieSession.fireAllRules();
        kieSession.dispose();
        
        System.out.println("Drools规则执行后，植物密度: " + plant.getPlantDensity() + ", 植物类型: " + plant.getPlantType());
        
        // 添加额外的检查，如果规则没有设置值，尝试直接根据植物名称设置默认值
        if (plant.getPlantDensity() == null || plant.getPlantType() == null) {
            System.out.println("Drools规则未设置植物密度或类型，尝试根据植物名称手动设置");
            // 根据free.drl中的规则添加几个常见植物的默认值
            if ("苜蓿".equals(plant.getPlantName())) {
                plant.setPlantDensity(1.5 / 667);
                plant.setPlantType(1);
            } else if ("无芒麦草".equals(plant.getPlantName())) {
                plant.setPlantDensity(2.0 / 667);
                plant.setPlantType(1);
            } else if ("沙棘".equals(plant.getPlantName())) {
                plant.setPlantDensity(0.6);
                plant.setPlantType(2);
            } else if ("油松".equals(plant.getPlantName())) {
                plant.setPlantDensity(0.4);
                plant.setPlantType(2);
            } else {
                System.out.println("未找到匹配的植物名称，设置默认草本植物值");
                plant.setPlantDensity(0.5 / 667);
                plant.setPlantType(1);
            }
            System.out.println("手动设置后，植物密度: " + plant.getPlantDensity() + ", 植物类型: " + plant.getPlantType());
        }
        
        return plant;
    }
}
